<template>
	<view>
		<navBar title="预约服务" background="#fff">
			<!-- <view slot="location" class="location acea-row row-middle" @click="addstore">
				<text class="iconfont icon-dingwei"></text>
				<view class="line1">{{ mer_name }}</view>
				<text class="iconfont icon-xiangyou"></text>
			</view> -->
		</navBar>
		<view class="beFixed" :style="{ paddingTop: titleH + 'px' }">
			<!-- 头部日期 -->
			<view class="top">
				<scroll-view scroll-x class="opten">
					<view v-for="(item, index) in appointmentDate" :key="index"
						:class="['top_time', index === active ? 'actives' : '']"
						@click="activeChanged(index, item.week)">
						<view>{{ item.date }}</view>
						<view>{{ item.week }}</view>
					</view>
				</scroll-view>
			</view>
			<view class="search acea-row row-between-wrapper">
				<view class="input acea-row row-between-wrapper">
					<text class="iconfont icon-sousuo2"></text>
					<input type="text" placeholder="搜索您要预约的医师名称" v-model="formDoctors.keyword" @confirm="searchHandle"
						confirm-type="search" @input="searchInp" />
				</view>
				<view class="bnt" @click="searchHandle">搜索</view>
			</view>
		</view>
		<!-- 医生列表 -->
		<view style="padding: 230rpx 18rpx 18rpx 18rpx;">
			<navigator
				:url="`/pages/shops/subs_appointment/doctorhome/index?id=${item.id}&week=${appointmentDate[active].week}`"
				class="box_doctor" v-for="(item, index) in listDoctors">
				<!-- 个人信息部分 -->
				<view class="doctor_information" v-if="listDoctors && listDoctors.length > 0">
					<view class="doctor_information_box">
						<view class="headportrait">
							<image :src="item.avatar"></image>
						</view>
						<view class="information">
							<view class="box">
								<view class="box_name">{{ item.name }}</view>
								<view class="position">{{ item.chief }}</view>
							</view>
							<view class="acea-row row-bottom row-between">
								<view class="types">{{ item.department_name }}</view>
								<view class="addbutton">预约</view>
							</view>
						</view>
					</view>
				</view>

				<!-- 技能 -->
				<view class="doctor_skill" @click="">{{ item.desc }}</view>
				<!-- <navigator url="/pages/shops/subs_appointment/doctorhome/index" hover-class="navigator-hover">
					
				</navigator> -->
			</navigator>
			<view class="loadingicon acea-row row-center-wrapper" v-if="listDoctors.length > 0">
				<text class="loading iconfont icon-jiazai" :hidden="loading == false"></text>
				{{ loadTitle }}
			</view>
			<block v-if="listDoctors.length == 0">
				<emptyPage title="暂无预约～"></emptyPage>
			</block>
		</view>
	</view>
</template>

<script>
	let app = getApp();
	var _self;
	import {
		doctorsListapi
	} from '@/api/appointment.js';
	import navBar from '../components/navBar/index.vue';
	import emptyPage from '@/components/emptyPage.vue';
	let systemInfo = uni.getSystemInfoSync();
	var marT = 4,
		titleH = systemInfo.statusBarHeight + 40,
		paddingTop = systemInfo.statusBarHeight;
	// #ifdef MP
	let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
	marT = menuButtonInfo.top - systemInfo.statusBarHeight;
	titleH = marT * 2 + menuButtonInfo.height + paddingTop;
	// #endif
	import {
		mapGetters
	} from "vuex";

	export default {
		computed: {
			...mapGetters(['routeError']),
		},
		components: {
			navBar,
			emptyPage
		},
		data() {
			return {
				titleH,
				active: 0,
				departmentid: '1',
				todayTime: null,
				loadend: false,
				loading: false,
				loadTitle: '加载更多',
				listDoctors: [], //医生列表
				appointmentDate: [],
				formDoctors: {
					page: 1,
					limit: 10,
					mer_id: '',
					service_time: '',
					department_id: '',
					keyword: ''
				},
				mer_name: ''
			};
		},
		onLoad(options) {
			if(this.routeError) {
				this.$store.commit('GO_ERROR');
				return
			}
			_self = this;
			this.formDoctors.department_id = options.id;
			this.getCurrentTime();
		},
		onShow() {
			var _this = this;
			uni.$once('merChange', function(data) {
				_this.formDoctors.mer_id = data.mer_id;
				_this.mer_name = data.mer_name;
				_this.doctorsList(true);
			});
		},
		mounted() {},
		methods: {
			searchInp(e) {
				var val = e.detail.value;
				if (!val) {
					this.searchHandle();
				}
			},
			searchHandle() {
				_self.doctorsList(true);
			},
			// 选中项改变的事件处理函数
			activeChanged(i, name) {
				this.active = i;
				this.formDoctors.service_time = name;
				this.formDoctors.page = 1;
				this.listDoctors = [];
				this.loadend = false;
				this.loading = false;
				this.loadTitle = '加载更多';
				this.doctorsList();
			},
			addstore() {
				uni.navigateTo({
					url: `/pages/shops/subs_appointment/store/index?mer_id=${this.formDoctors.mer_id}`
				});
			},
			/**
			 * 获取医生列表
			 */
			doctorsList: function(isPage) {
				if (isPage === true) {
					_self.$set(_self, 'listDoctors', []);
					_self.formDoctors.page = 1;
					_self.listDoctors = [];
					_self.loadend = false;
					_self.loading = false;
					_self.loadTitle = '加载更多';
				}
				if (_self.loadend) return;
				if (_self.loading) return;
				_self.loading = true;
				_self.loadTitle = '';
				doctorsListapi(this.formDoctors)
					.then(res => {
						const {
							list,
							mer_id,
							mer_name
						} = res.data;
						_self.formDoctors.mer_id = mer_id;
						_self.mer_name = mer_name;
						let listDoctors = _self.$util.SplitArray(list.data, _self.listDoctors);
						let loadend = list.data.length < _self.formDoctors.limit;
						_self.loadend = loadend;
						_self.loading = false;
						_self.loadTitle = loadend ? '已全部加载' : '加载更多';
						_self.$set(_self, 'listDoctors', listDoctors);
						_self.$set(_self.formDoctors, 'page', _self.formDoctors.page + 1);
					})
					.catch(err => {
						_self.loading = false;
						_self.loadTitle = '加载更多';
					});
			},
			// 触底的事件
			onReachBottom() {
				this.doctorsList();
			},
			getCurrentTime() {
				//获取当前时间
				let myDate = new Date();
				var timesStamp = myDate.getTime();
				var currenDay = myDate.getDay();
				var weeks = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
				var dates = [];
				for (var i = 0; i < 7; i++) {
					var month = new Date(timesStamp + 24 * 60 * 60 * 1000 * i).getMonth() + 1;
					var day = new Date(timesStamp + 24 * 60 * 60 * 1000 * i).getDate();
					var month = new Date(timesStamp + 24 * 60 * 60 * 1000 * i).getMonth() + 1;
					month = month < 10 ? `0${month}` : month;
					var day = new Date(timesStamp + 24 * 60 * 60 * 1000 * i).getDate();
					day = day < 10 ? `0${day}` : day;
					var weekI = new Date(timesStamp + 24 * 60 * 60 * 1000 * i).getDay();
					dates.push({
						date: month + '-' + day,
						week: weeks[weekI]
					});
				}
				this.appointmentDate = dates;
				this.formDoctors.service_time = this.appointmentDate[0].week;
				this.doctorsList(true);
			}
		}
	};
</script>

<style lang="scss">
	.location {
		font-size: 24rpx;
		margin-left: 40rpx;
		line-height: 1;
		flex: 1;
		overflow: hidden;

		.iconfont {
			font-size: 22rpx;
		}

		.line1 {
			margin: 0 10rpx;
			max-width: calc(100% - 80rpx);
		}
	}

	.top {
		background: #fff;

		.actives {
			background-color: $yy-main-col;

			view {
				color: #ffffff;
			}
		}

		.opten {
			display: flex;
			white-space: nowrap;
			border-top: solid 1rpx #f5f5f5;
			border-bottom: solid 1rpx #f5f5f5;
			box-sizing: border-box;
			height: 110rpx;
		}
	}

	.top_time {
		width: 125rpx;
		height: 110rpx;
		display: inline-flex;
		flex-wrap: wrap;
		background-color: #fff;
		justify-content: center;
		align-items: center;
		padding: 15rpx 0;
		border-right: solid 1rpx #f5f5f5;

		&:last-child {
			border-right: none;
		}

		view {
			font-size: 28upx;
			color: #a3a0a1;
			z-index: 99;
		}
	}

	// 医生列表样式
	.box_doctor {
		background-color: #ffffff;
		border-radius: 20upx;
		width: 100%;
		padding: 25rpx 22rpx 30rpx 35rpx;
		margin-bottom: 30rpx;
		box-sizing: border-box;

		.doctor_information {
			.doctor_information_box {
				width: 100%;
				display: flex;
			}

			.headportrait {
				width: 113rpx;
				height: 113rpx;
				margin-right: 30rpx;

				image {
					width: 100%;
					height: 100%;
					border-radius: 50%;
					overflow: hidden;
				}
			}

			.information {
				padding-top: 26rpx;
				line-height: 1;
				flex: 1;
				overflow: hidden;

				.box {
					.box_name {
						color: #000;
						margin-right: 5rpx;
						font-size: 34rpx;
						font-weight: 700;
						display: inline-block;
					}

					.position {
						display: inline-block;
						color: #3e3e3e;
						font-size: 26rpx;
					}
				}

				.types {
					color: $yy-main-col;
					margin-top: 18upx;
					font-size: 24rpx;
				}

				.addbutton {
					background-color: $yy-main-col;
					color: #fff;
					font-size: 24rpx;
					text-align: center;
					line-height: 40rpx;
					height: 40rpx;
					border-radius: 20rpx;
					padding: 0 30rpx;
				}
			}
		}

		.doctor_skill {
			color: #9c9c9c;
			margin-top: 22rpx;
			font-size: 24rpx;
			line-height: 1;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}

	.search {
		padding: 10rpx 0 10rpx 30rpx;
		background: #fff;

		.input {
			width: 598rpx;
			background-color: #f7f7f7;
			border-radius: 32rpx;
			padding: 0 36rpx;
			box-sizing: border-box;
			height: 66rpx;

			.iconfont {
				color: #000;
				font-size: 36rpx;
			}

			input {
				width: 472rpx;
				font-size: 28rpx;
			}
		}

		.bnt {
			width: 120rpx;
			text-align: center;
			height: 66rpx;
			line-height: 66rpx;
			font-size: 30rpx;
			color: #282828;
		}
	}

	.beFixed {
		position: fixed;
		top: 0;
		width: 100%;
		z-index: 9;
		left: 0;
		box-sizing: border-box;
	}
</style>
